package com.example.lifecycle.LifeCycleObserver;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;

import com.example.lifecycle.R;

public class ChronometerActivity extends AppCompatActivity {

    //private MyChronometer chronometer;
    private long elapseTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chronometer);

        //LifeCycle进行解耦
        //获取chronomerter对象
        MyChronometer myChronometer = findViewById(R.id.chronometer);
        //添加观察器
        //getLifecycle是创建的被观察者对象 myChronometer是创建的观察者对象用于监听被观察者的生命周期
        getLifecycle().addObserver(myChronometer);
    }

    //正常做法
   /* @Override
    protected void onResume() {
        super.onResume();
        chronometer.setBase(SystemClock.elapsedRealtime() - elapseTime);
        chronometer.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        elapseTime = (SystemClock.elapsedRealtime()) - chronometer.getBase();
        chronometer.stop();
    }*/
}